define(["jquery","servers"], function ($,servers) {
    return Backbone.Model.extend({
        defaults: function () {
            return {
                defListStatus:[{id:"1",name:"长期投放"},{ id: "2", name: "时段投放"}]
            };
        },
        getPageList:function(param){
            var self = this;
            var paramMap=servers.getPageParamMap();
            paramMap= $.ExtCommon.initMergeJson(paramMap,{advertiserId:"customer",startDate:"startTime",endDate:"endTime"});
            var newParam= $.ExtCommon.getMappingJson(param,paramMap);
            servers.campaign.pageList(newParam,function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data.list,function (info) {
                        var newInfo= $.ExtCommon.getMappingJson(info,{
                            id:"id",name:"name",startTime:"startTime",endTime:"endTime",
                            pv:"pv",click:"click",cr:"cr", unitPrice:"cpc",cost:"cost",
                            totalCost:"totalCost", budget:"budget"
                        },true);
                        if(!$.ExtCommon.isCheckEmpty(newInfo.startTime)){newInfo.startTime=$.ExtMoment.initFormat(newInfo.startTime)}
                        newInfo.endTime=$.ExtCommon.isCheckEmpty(newInfo.endTime)?"长期":$.ExtMoment.initFormat(newInfo.endTime);
                        $.ExtCommon.conversionEmptyZero(newInfo,["pv","click","cost","cr","unitPrice"]);
                        newInfo.ratioBudget= $.ExtCommon.getValueRatio(newInfo.totalCost,newInfo.budget);
                        return newInfo;
                    });
                }else{
                    res.data={};
                }
                res.dataOriginal=res.data;
                res.data.list=newJson;
                self.set({dataPageList: res});
                self.trigger('bind:PageList');
            });
        },
        getInfo: function (id) {
            var self = this;
            if (id) {
                servers.campaign.info(id, function (res) {
                    var newJson = $.ExtCommon.getMappingJson(res.success ? res.data : {}, {
                        id: "id",name: "name",customer: "advertiserId",description: "description",startTime:"startTime",endTime:"endTime",
                        resourceType:"platformId",budget:"budget",chargePrice:"dayBudget"
                    }, true);
                    newJson.startTime=$.ExtCommon.isCheckEmpty(newJson.startTime)?$.ExtMoment.initFormat():$.ExtMoment.initFormat(newJson.startTime);

                    if($.ExtCommon.isCheckEmpty(newJson.endTime)){
                        newJson.endTime="";
                        newJson.timeType="1"
                    }else{
                        newJson.endTime=$.ExtMoment.initFormat(newJson.endTime);
                        newJson.timeType="2"
                    }
                    newJson.timeType=$.ExtCommon.isCheckEmpty(newJson.endTime)?"1":"2";
                    newJson.isLong=newJson.timeType==="1";
                    newJson.isRange=newJson.timeType==="2";
                    res.dataOriginal = res.data;
                    res.data = newJson;
                    self.set({dataInfo: {isAdd: false, res: res}});
                    self.trigger('bind:Info');
                });
            }else {
                var dataDef={timeType:1,isLong:true,isRange:false,startTime:$.ExtMoment.initFormat(),chargePrice:10000};
                self.set({dataInfo:{isAdd:true,res:servers.getSendSuccess(dataDef)}});
                self.trigger('bind:Info');
            }
        },
        addInfo: function (json) {
            var self = this;
            var data=this._getMappingToServer(json);
            servers.campaign.add(data, function (res) {
                self.set({dataSave: {isAdd: true, req: json, res: res}});
                self.trigger('save:Info');
            });
        },
        updateInfo: function (json) {
            var self = this;
            var data=this._getMappingToServer(json);
            data.id=json.id;
            servers.campaign.update(data, function (res) {
                self.set({dataSave: {isAdd: false, req: json, res: res}});
                self.trigger('save:Info');
            });
        },
        _getMappingToServer:function(json){
            var newJson = $.ExtCommon.getMappingJson(json, {
                name: "name",advertiserId: "customer",description: "description",startTime:"startTime",endTime:"endTime",platformId:"resourceType",budget:"budget",dayBudget:"chargePrice"
            });
            newJson.startTime += " 00:00:00";
            if(!$.ExtCommon.isCheckEmpty(newJson.endTime)) {newJson.endTime += " 23:59:59"}
            return newJson;
        }
    });
});